package com.ywjt.platformbusiness.store.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.store.activity.StoreAddGoodsActivity;

import java.util.ArrayList;
import java.util.List;

public class GoodsManagerFragment extends BaseVPFragment implements View.OnClickListener {

    private View view;
    private Context context;
    private LinearLayout llUp,llDown;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private MainAdapter adapter;
    private TextView tvUp,tvDown,tvLineDown,tvLineUp,tvAddGoods;

    public static GoodsManagerFragment newInstance() {
        Bundle arguments = new Bundle();
        GoodsManagerFragment fl = new GoodsManagerFragment();
        fl.setArguments(arguments);
        return fl;
    }



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.goodmanage_fragment, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llUp:
                viewPager.setCurrentItem(0);
                tvLineUp.setVisibility(View.VISIBLE);
                tvLineDown.setVisibility(View.INVISIBLE);
                tvUp.setTextColor(context.getResources().getColor(R.color.red));
                tvDown.setTextColor(context.getResources().getColor(R.color.gray9));
                break;
            case R.id.llDown:
                viewPager.setCurrentItem(1);
                tvLineDown.setVisibility(View.VISIBLE);
                tvLineUp.setVisibility(View.INVISIBLE);
                tvDown.setTextColor(context.getResources().getColor(R.color.red));
                tvUp.setTextColor(context.getResources().getColor(R.color.gray9));
                break;
            case R.id.tvAddGoods:
                StoreAddGoodsActivity.actionStart(context);
                break;
        }
    }

    public void initView() {
        llUp=view.findViewById(R.id.llUp);
        tvUp=view.findViewById(R.id.tvUp);
        tvLineDown=view.findViewById(R.id.tvLineDown);
        tvLineUp=view.findViewById(R.id.tvLineUp);
        tvDown=view.findViewById(R.id.tvDown);
        llUp.setOnClickListener(this::onClick);
        llDown=view.findViewById(R.id.llDown);
        viewPager=view.findViewById(R.id.viewPager);
        tvAddGoods=view.findViewById(R.id.tvAddGoods);
        tvAddGoods.setOnClickListener(this::onClick);
        llDown.setOnClickListener(this::onClick);
        fragmentList=new ArrayList<>();
        fragmentList.add(GoodUpTypeFragment.newInstance(0));
        fragmentList.add(GoodUpTypeFragment.newInstance(1));

        adapter=new MainAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        tvLineUp.setVisibility(View.VISIBLE);
                        tvLineDown.setVisibility(View.INVISIBLE);
                        tvUp.setTextColor(context.getResources().getColor(R.color.red));
                        tvDown.setTextColor(context.getResources().getColor(R.color.gray9));
                        tvAddGoods.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        tvLineDown.setVisibility(View.VISIBLE);
                        tvLineUp.setVisibility(View.INVISIBLE);
                        tvDown.setTextColor(context.getResources().getColor(R.color.red));
                        tvUp.setTextColor(context.getResources().getColor(R.color.gray9));
                        tvAddGoods.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    /**
     * 在fragment首次可见时回调，可用于加载数据，防止每次进入都重复加载数据
     */
    @Override
    protected void onFragmentFirstVisible() {

    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentList.get(position).toString();
        }
    }
}
